package com.cskaoyan.market.validator;

import com.cskaoyan.market.constrain.MarketAdminConflictUsername;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author lixianbiao
 * @since 2024/06/28 20:14
 */
@Component
public class MarketAdminUsernameConflictValidator implements ConstraintValidator<MarketAdminConflictUsername, MarketAdmin> {
    @Autowired
    MarketAdminMapper adminMapper;

   /* @Autowired
    public MarketAdminConflictUsernameValidator(MarketAdminMapper adminMapper){
        this.adminMapper = adminMapper;
    }
*/

    @Override
    public boolean isValid(MarketAdmin value, ConstraintValidatorContext constraintValidatorContext) {
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(value.getUsername());
        MarketAdmin admin = adminMapper.selectOneByExample(adminExample);
        if(admin != null && !admin.getId().equals(value.getId())){
            return false;
        }
        return true;
    }
}